package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.SupplierContactDao;
import cn.gson.boot.model.dao.SupplierDao;
import cn.gson.boot.model.mapper.SupplierContactMapper;
import cn.gson.boot.model.mapper.SupplierMapper;
import cn.gson.boot.model.pojos.Supplier;
import cn.gson.boot.model.pojos.SupplierContact;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SupplierService {
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    SupplierDao supplierDao;
    @Autowired
    SupplierContactDao supplierContactDao;

    //供应山分页查询
    public List<Supplier> selectAllSuppliers(String search) {
        return supplierMapper.selectAllSuppliers(search);
    }

    //新增供应商
    public void addSupplier(Supplier supplier) {
        supplier.setSupplierSc(0);
        supplierDao.save(supplier);
    }

    //修改供应商
    public List<Supplier> upSupplier(Integer supplierId) {
        return supplierMapper.upSupplier(supplierId);
    }

    //删除供应商
    public void delSupplier(Integer supplierId) {
        Supplier supplier = supplierDao.findById(supplierId).get();
        List<SupplierContact> contacts = supplierMapper.selectContacts(supplierId);
        SupplierContact supContact = null;
        for(int i=0;i<contacts.size();i++){
            supContact = supplierContactDao.findById(contacts.get(i).getSupcontactId()).get();
            supContact.setSupcontactSc(1);
            supplierContactDao.save(supContact);
        }
        supplier.setSupplierSc(1);
        supplierDao.save(supplier);
    }


}
